<?php

namespace app\api\controller\business;

use think\Db;

/**
 * 列表
 * Class Lists
 * @package app\api\controller\business
 * 
 * @method array tags($typs)										标签切换内容
 * @method array newlyGift($uid, $typs, $offset, $limit)			新人有礼活动达标
 * @method array feedback($uid, $typs, $offset, $limit)				反馈列表
 */
class Lists
{

	/**
	 * 实例对象
	 * @var object
	 */
	protected static $instance;

	/**
	 * @var \app\commom\model\User
	 */
	protected $userModel = null;

	/**
	 * @var \app\commom\model\QuestionContent
	 */
	protected $quesModel = null;

	/**
	 * @var \app\commom\model\QuestionContent
	 */
	protected $quModel = null;

	/**
	 * @var \app\commom\model\ActivityTake
	 */
	protected $takesModel = null;

	/**
	 * @var \app\commom\model\Admin
	 */
	protected $adminModel = null;

	/**
	 * @var \app\commom\model\Activity
	 */
	protected $actvityModel = null;

	/**
	 * @var \app\commom\model\Address
	 */
	protected $addrModel = null;

	public function __construct()
	{
		$this->addrModel =  new \app\common\model\Address;
		$this->adminModel = new \app\common\model\Admin;
		$this->userModel = new \app\common\model\User;
		$this->quModel = new \app\admin\model\Question;
		$this->takesModel = new \app\common\model\ActivityTake;
		$this->actvityModel = new \app\common\model\Activity;
		$this->quesModel = new \app\common\model\QuestionContent;
	}

	/**
	 * 实例化
	 */
	public static function instance()
	{
		if (is_null(self::$instance)) {
			self::$instance = new static();
		}
		return self::$instance;
	}

	/**
	 * 标签切换
	 */
	public function tags($typs)
	{
		$return = [];
		if ($typs) {
			switch ($typs) {
				case 5:
					$return = [["id" => 1, "name" => "已处理"], ["id" => 2, "name" => "待处理"]];
					break;
				case 4:
					$return = [["id" => 1, "name" => "已分配"], ["id" => 2, "name" => "待分配"]];
					break;
				case 3:
					$return = [["id" => 1, "name" => "已完成"], ["id" => 2, "name" => "未完成"]];
					break;
				default:
					$return = [];
					break;
			}
		}
		return $return;
	}

	/**
	 * 用户 等级 - 归类显示条件
	 */
	public function rankClass($lev, $typs, $uid)
	{
		switch ($lev) {
			case 5: // 客服
				return ["where" => "process = {$typs}", "order" => "stands_time ASC"];;
				break;
			case 4:	// 业务主管
				return ["where" => "process = 1 and  is_allot = {$typs}", "order" => "is_allot DESC,process_time ASC"];;
				break;
			case 3: // 业务
				return ["where" => "process = 1 and  is_allot = {$typs} and  allot_uid = {$uid}", "order" => "allot_time DESC"];
				break;
			default:
				return [];
				break;
		}
	}

	/**
	 * 新人有礼活动达标
	 */
	public function newlyGift($uid, $typs, $offset, $limit)
	{
		$ret = ["data" => [], "total" => 0];
		if ($uid && $typs) {
			$userLevel = $this->userModel->where("id", $uid)->value("is_typs");
			$rank = $this->rankClass($userLevel, $typs, $uid);
			if ($rank) {
				$total = $this->takesModel
					->where($rank["where"])
					->where("is_stands", 1)
					->count();

				$field = "id,uid,active_typs,tranAmt,standsAmt,is_stands,act_id,process_id,process_uid,remark";
				$field .= ",from_unixtime(start , '%Y-%m-%d') start,from_unixtime(ends , '%Y-%m-%d') ends,from_unixtime(stands_time , '%Y-%m-%d') standsTime,from_unixtime(create_time,'%Y-%m-%d') createTime,process,if(process_time = 0 , 0 , from_unixtime(process_time, '%Y-%m-%d')) processTime";
				$field .= ",nickname,mobile allPhone,insert(mobile,4,4,'****') hidPhone";
				$field .= ",allot_uid,is_allot,if(allot_time = 0 , 0 , from_unixtime(allot_time, '%Y-%m-%d') ) allotTime";

				$list = $this->takesModel
					->alias("a")
					->join("(select id ids,nickname,mobile from fd_user) b", 'a.uid = b.ids', 'left')
					->where($rank["where"])
					->where("is_stands", 1)
					->field($field)
					->order($rank['order'])
					->limit($offset, $limit)
					->select();
				$list = collection($list)->toArray();
				if ($list) {
					foreach ($list as $key => $vals) {
						$list[$key]["standsNick"] = $vals["is_stands"] == 1 ? "已达标" : "未达标";
						$list[$key]["processNick"] = $vals["process"] == 1 ? "已处理" : "未处理";
						$list[$key]["activity"] = $this->actvityModel->where("id", $vals["act_id"])->value("title");
						$list[$key]["pcAdmin"]  = $vals["process_id"] ? $this->adminModel->where("id", $vals["process_id"])->value("nickname") : "";
						$list[$key]["appAdmin"] = $vals["process_uid"] ? $this->userModel->where("id", $vals["process_uid"])->value("nickname") : "";
						$list[$key]["address"] = $this->addrModel->where("user_id", $vals['uid'])->value("concat(province ,city,district,address)");
						$list[$key]["allotAgent"] = $vals["allot_uid"] ? $this->userModel->where("id", $vals["allot_uid"])->value("nickname") : "";
						$list[$key]["allotState"] = $vals["is_allot"] == 1 ? "已分配" : "未分配";
					}
				}
				$ret = ["data" => $list, "total" => $total];
			}
		}
		return $ret;
	}

	/**
	 * 客户反馈
	 * @param  $name 角色组ID
	 */
	public function feedback($uid, $typs, $offset, $limit)
	{
		$ret = ["data" => [], "total" => 0];
		if ($uid && $typs) {
			// 后端角色ID
			$grouid = $this->userModel->where("id", $uid)->value("group_id");
			if ($grouid) {
				// 处理分类
				$buildSql = $this->quModel->where("dep_id", $grouid)->field("id")->buildSql();
				// 列
				$total = $this->quesModel
					->alias("a")
					->join("(select id ids from fd_user) b", 'a.uid = b.ids', 'left')
					->where("class_id in {$buildSql}")
					->where("process", $typs)
					->count();

				$ret = $this->quesModel
					->alias("a")
					->join("(select id ids,nickname,mobile from fd_user) b", 'a.uid = b.ids', 'left')
					->where("class_id in {$buildSql}")
					->where("process", $typs)
					->field("id,process,uid,class_id,remark,create_time,nickname,mobile allPhone,insert(mobile,4,4,'****') hidPhone")
					->order(($typs == 1 ? "process_time DESC," : "") . "create_time ASC ")
					->limit($offset, $limit)
					->select();
				$list = collection($ret)->toArray();
				if ($list) {
					$quClass = $this->quModel->column("title", "id");
					foreach ($list as $key => $vals) {
						$list[$key]["quClass"] = $quClass[$vals["class_id"]];
						$list[$key]["processNick"] = $vals["process"] == 1 ? "已处理" : "未处理";
					}
				}
				$ret = ["data" => $list, "total" => $total];
			}
		}
		return $ret;
	}
}
